package com.kuang.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 自定义拦截器
 */
public class LoginHandlerInterceptor implements HandlerInterceptor {
    /*
        登录拦截器
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 获取用户名称，登录成功之后，应该有用户的session
        Object inputEmail = request.getSession().getAttribute("email");
        // 登录失败返会登录页
        if (inputEmail == null) {
            request.setAttribute("msg", "没有权限，请重新登录");
            request.getRequestDispatcher("/").forward(request, response);
            return false;
        } else {
            return true;
        }
    }
}
